#!/usr/bin/python
#coding: utf-8
#Boa:Dialog:KEHUXX_DLG

import wx
import current

def create(parent):
    return KEHUXX_DLG(parent)

[wxID_KEHUXX_DLG, wxID_KEHUXX_DLGADDR, wxID_KEHUXX_DLGGSNAME, 
 wxID_KEHUXX_DLGNAME, wxID_KEHUXX_DLGONCLICKCANLE, wxID_KEHUXX_DLGONCLICKOK, 
 wxID_KEHUXX_DLGPHONE, wxID_KEHUXX_DLGSTATICTEXT1, wxID_KEHUXX_DLGSTATICTEXT2, 
 wxID_KEHUXX_DLGSTATICTEXT3, wxID_KEHUXX_DLGSTATICTEXT4, 
] = [wx.NewId() for _init_ctrls in range(11)]

class KEHUXX_DLG(wx.Dialog):
    modify=False
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_KEHUXX_DLG, name=u'KEHUXX_DLG',
              parent=prnt, pos=wx.Point(417, 225), size=wx.Size(404, 353),
              style=wx.DEFAULT_DIALOG_STYLE, title=u'\u5ba2\u6237\u4fe1\u606f')
        self.SetClientSize(wx.Size(396, 319))

        self.staticText1 = wx.StaticText(id=wxID_KEHUXX_DLGSTATICTEXT1,
              label=u'\u59d3      \u540d\uff1a', name='staticText1',
              parent=self, pos=wx.Point(40, 56), size=wx.Size(60, 14), style=0)

        self.staticText2 = wx.StaticText(id=wxID_KEHUXX_DLGSTATICTEXT2,
              label=u'\u7535\u8bdd\u53f7\u7801\uff1a', name='staticText2',
              parent=self, pos=wx.Point(40, 96), size=wx.Size(60, 14), style=0)

        self.staticText3 = wx.StaticText(id=wxID_KEHUXX_DLGSTATICTEXT3,
              label=u'\u5730      \u5740\uff1a', name='staticText3',
              parent=self, pos=wx.Point(40, 136), size=wx.Size(60, 14),
              style=0)

        self.staticText4 = wx.StaticText(id=wxID_KEHUXX_DLGSTATICTEXT4,
              label=u'\u5355\u4f4d\u5730\u5740\uff1a', name='staticText4',
              parent=self, pos=wx.Point(40, 176), size=wx.Size(60, 14),
              style=0)

        self.name = wx.TextCtrl(id=wxID_KEHUXX_DLGNAME, name=u'name',
              parent=self, pos=wx.Point(112, 48), size=wx.Size(160, 22),
              style=0, value=u'')

        self.phone = wx.TextCtrl(id=wxID_KEHUXX_DLGPHONE, name=u'phone',
              parent=self, pos=wx.Point(112, 88), size=wx.Size(160, 22),
              style=0, value=u'')

        self.addr = wx.TextCtrl(id=wxID_KEHUXX_DLGADDR, name=u'addr',
              parent=self, pos=wx.Point(112, 128), size=wx.Size(160, 22),
              style=0, value=u'')

        self.gsname = wx.TextCtrl(id=wxID_KEHUXX_DLGGSNAME, name=u'gsname',
              parent=self, pos=wx.Point(112, 168), size=wx.Size(160, 22),
              style=0, value=u'')

        self.OnClickOk = wx.Button(id=wxID_KEHUXX_DLGONCLICKOK,
              label=u'\u786e\u5b9a', name=u'OnClickOk', parent=self,
              pos=wx.Point(88, 224), size=wx.Size(75, 24), style=0)
        self.OnClickOk.Bind(wx.EVT_BUTTON, self.OnOnClickOkButton,
              id=wxID_KEHUXX_DLGONCLICKOK)

        self.OnClickCanle = wx.Button(id=wxID_KEHUXX_DLGONCLICKCANLE,
              label=u'\u53d6\u6d88', name=u'OnClickCanle', parent=self,
              pos=wx.Point(224, 224), size=wx.Size(75, 24), style=0)
        self.OnClickCanle.Bind(wx.EVT_BUTTON, self.OnOnClickCanleButton,
              id=wxID_KEHUXX_DLGONCLICKCANLE)

    def __init__(self, parent):
        self._init_ctrls(parent)
        self.findname=None
        
    def SetKehuxx(self,kehuxx):        
        self.name.SetValue(self.kehucurrent.name)
        self.phone.SetValue(self.kehucurrent.phone)
        self.addr.SetValue(self.kehucurrent.addr)
        self.gsname.SetValue(self.kehucurrent.gsname)
        
    def GetKehuxx(self): 
        self.kehucurrent=current.kehuxx('a')
        self.kehucurrent.name=self.name.GetValue()
        self.kehucurrent.phone=self.phone.GetValue()
        self.kehucurrent.addr=self.addr.GetValue()
        self.kehucurrent.gsname=self.gsname.GetValue()  

    def OnOnClickOkButton(self, event):
        session=current.CreateSession()
        if KEHUXX_DLG.modify==True:
            KEHUXX_DLG.modify=False
            wx.MessageBox(u'修改')
            KEHUXX_DLG.modify=False
            curname=self.name.GetValue()
            #cur=session.query(current.cpxx).filter_by(**{'mingch':self.tempmingch}).first() 
            self.kehucurrent=session.query(current.kehuxx).filter_by(**{'name':self.findname}).first()
            #self.GetKehuxx() #代替以下方法不能更改数据库，Why?
            self.kehucurrent.name=self.name.GetValue()
            self.kehucurrent.phone=self.phone.GetValue()
            self.kehucurrent.addr=self.addr.GetValue()
            self.kehucurrent.gsname=self.gsname.GetValue()
        else:            
            self.GetKehuxx()            
            session=current.CreateSession()
            current.InsertOb(self.kehucurrent)            
        current.CommitSession(session)     
        current.CloseSession(session)
        wx.MessageBox(u'完成!')
        self.Destroy()
        
        

    def OnOnClickCanleButton(self, event):
        self.Destroy()

def test():
    app=wx.PySimpleApp()
    class fr(wx.Frame):
        def __init__(self):
            wx.Frame.__init__(self,None)
            but=wx.Button(self)
            but.Bind(wx.EVT_BUTTON,self.OnBut)
        def OnBut(self,event):
            f=create(ff)
            f.ShowModal()
    ff=fr()
    ff.Show()
    app.MainLoop()
